package com.encyclopedia.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.encyclopedia.entity.Family;
import com.encyclopedia.entity.Genus;
import com.encyclopedia.entity.Plant;
import com.encyclopedia.mapper.PlantMapper;
import com.encyclopedia.result.exception.ServiceException;
import com.encyclopedia.result.exenum.RequestException;
import com.encyclopedia.service.FamilyService;
import com.encyclopedia.service.GenusService;
import com.encyclopedia.service.PlantService;
import com.encyclopedia.service.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PlantServiceImpl implements PlantService {
    @Autowired
    private PlantMapper plantMapper;
    @Autowired
    private GenusService genusService;
    @Autowired
    private FamilyService familyService;
    @Autowired
    private RedisUtils redisUtils;
    private final Integer pageData = 9;


    @Override
    public List<Plant> ByName(String name) {
        String key = "PlantServiceImplByName:" + name;
        if (name == null) throw new ServiceException(RequestException.Plant_ParamNull);
        List<Plant> plantRedis = (List<Plant>) redisUtils.get(key);
        if (plantRedis != null) return plantRedis;
        List<Plant> plantByName = plantMapper.getPlantByName(name);
        redisUtils.set(key, plantByName, 60 * 60 * 12);
        if (plantByName == null) throw new ServiceException(RequestException.NOT_FOUND);

        return plantByName;
    }

    @Override
    public List<Plant> ByGenusId(Integer genusId) {
        if (genusId == null) throw new ServiceException(RequestException.Plant_ParamNull);
        String key = "PlantServiceImplByGenusId" + genusId;
        List<Plant> plantss = (List<Plant>) redisUtils.get(key);
        if (plantss != null) return plantss;
        List<Plant> plants = plantMapper.selectByGenusId(genusId);
        redisUtils.set(key, plants, 60 * 60 * 12);
        if (plants == null) throw new ServiceException(RequestException.NOT_FOUND);
        return plants;
    }

    @Override
    public Plant ById(Integer PlantId) {
        if (PlantId == null) throw new ServiceException(RequestException.Plant_ParamNull);
        String key = "PlantServiceImplById" + PlantId;
        Plant plant = (Plant) redisUtils.get(key);
        if (plant != null) return plant;
        Plant plantOne = plantMapper.getPlantById(PlantId);
        redisUtils.set(key, plantOne, 60 * 60 * 12);
        if (plantOne == null) {
            throw new ServiceException(RequestException.NOT_FOUND);
        }
        return plantOne;
    }

    /**
     * 根据属名查询
     *
     * @param genusName
     * @return
     */

    @Override
    public List<Plant> getPlantByGenusName(String genusName) {
        if (genusName == null) throw new ServiceException(RequestException.Plant_ParamNull);
        String key = "PlantServiceImplByGenusName:" + genusName;
        List<Plant> plants = (List<Plant>) redisUtils.get(key);
        if (plants != null) return plants;
        List<Genus> genusOne = genusService.getGenusByName(genusName);
        List<Integer> ids = genusOne.stream().map(Genus::getGenusId).toList();
        if (genusOne.isEmpty()) throw new ServiceException(RequestException.NOT_FOUND);
        List<Plant> plantByGenusIda = plantMapper.getPlantByGenusIda(ids);
        redisUtils.set(key, plantByGenusIda, 60 * 60 * 12);
        if (plantByGenusIda.isEmpty()) throw new ServiceException(RequestException.NOT_FOUND);

        return plantByGenusIda;

    }

    @Override
    public List<Plant> getPlantByFamilyName(String familyName) {
        if (familyName == null) throw new ServiceException(RequestException.Plant_ParamNull);
        String key = "PlantServiceImplByFamilyName:" + familyName;
        List<Plant> plants = (List<Plant>) redisUtils.get(key);
        if (plants != null) return plants;
        List<Family> familyList = familyService.getFamilyByName(familyName);
        if (familyList == null) throw new ServiceException(RequestException.NOT_FOUND);
        List<Integer> list = familyList.stream().map(Family::getFamilyId).toList();
        List<Plant> plantByFamilyIda = plantMapper.getPlantByFamilyIda(list);
        redisUtils.set(key, plantByFamilyIda, 60 * 60 * 12);
        if (plantByFamilyIda.isEmpty()) throw new ServiceException(RequestException.NOT_FOUND);


        return plantByFamilyIda;
    }

    @Override
    public List<Plant> getPlantByMonth(Integer month) {
        String s = "春";
        if (month == null) throw new ServiceException(RequestException.Plant_ParamNull);
        if (month < 1 || month > 12) throw new ServiceException(RequestException.NOT_FOUND);

        if (month >= 6 && month <= 8) {
            s = "夏";
        } else if (month >= 9 && month <= 11) {
            s = "秋";
        } else if (month >= 12 || month <= 2) {
            s = "冬";
        }
        String key = "PlantServiceImplByMonth" + s;
        List<Plant> plants = (List<Plant>) redisUtils.get(key);
        if (plants != null) return plants;
        List<Plant> plantss = plantMapper.getPlantByMonth(s);
        redisUtils.set(key, plantss, 60 * 60 * 12);
        if (plantss.isEmpty()) throw new ServiceException(RequestException.NOT_FOUND);
        return plantss;
    }

    @Override
    public Page getPlantAll(Integer current) {
        if (current == null) throw new ServiceException(RequestException.Plant_ParamNull);
        String key = "PlantServiceImplPlantAllCurrent:" + current;
        if (current > 0 && current != 1) current = current * pageData;
        else current = 0;
        List<Plant> plants = plantMapper.getLimitPlant(current, pageData);
        List<Integer> ids = plants.stream().map(Plant::getId).collect(Collectors.toList());
        if (ids.size() == 0) throw new ServiceException(RequestException.NOT_FOUND);
        List<Plant> allByIdList = plantMapper.getAllByIdList(ids);
        redisUtils.set(key, allByIdList, 60 * 60 * 12);
        if (allByIdList.isEmpty()) throw new ServiceException(RequestException.NOT_FOUND);
        Page<Plant> page = new Page<>();
        page.setRecords(allByIdList);
        page.setTotal(plantMapper.selectCount(null));
        return page;
    }


}
